<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RoomFrame extends Base
{
    protected $table = 'room_frame';
    protected $roomFrameLogTable = 'room_frame_log';

    //房间边框缓存
    protected $roomFrameKey = 'ROOMFRAME:DATA';//string ROOMFRAME:DATA => {string}
    //房间边框房间缓存
    protected $roomFrameRoomKey = 'ROOMFRAME:USERDATA:';//string ROOMFRAME:USERDATA:{roomId} => {string}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //新增房间边框
    public function addRoomFrame($frameName, $frameImg, $frameDesc)
    {
        $data = [];
        $data['frame_name'] = $frameName;
        $data['frame_img'] = getUrlPath($frameImg);
        $data['frame_desc'] = $frameDesc;

        $id = DB::table($this->table)->insertGetId($data);

        $cacheKey = $this->roomFrameKey;
        redis::DEL($cacheKey);

        return $id;
    }

    //编辑房间边框
    public function editRoomFrame($frameId, $params)
    {
        $upData = [];
        if(!empty($params['frame_name'])) {
            $upData['frame_name'] = $params['frame_name'];
        }
        if(!empty($params['frame_img'])) {
            $upData['frame_img'] = getUrlPath($params['frame_img']);
        }
        if(!empty($params['frame_desc'])) {
            $upData['frame_desc'] = getUrlPath($params['frame_desc']);
        }
        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->table)->where('id', $frameId)->where('is_delete', 0)->update($upData);

        $cacheKey = $this->roomFrameKey;
        redis::DEL($cacheKey);

        return $res;
    }

    /**
     * 根据id获取房间边框
     */
    public function getRoomFrameById($frameId){
        $data = DB::table($this->table)
            ->where('is_delete', 0)
            ->where('id', $frameId)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 获取房间边框数据
     */
    public function getRoomFrameAll(){
        $cacheKey = $this->roomFrameKey;
        $list = Redis::get($cacheKey);
        if(empty($list)){
            $list = DB::table($this->table)
                ->where('is_delete', 0)
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($cacheKey, 86400, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }
        foreach($list as $key=>$val) {
            $list[$key]['frame_img'] = build_url($val['frame_img']);
            $list[$key] = $val;
        }
        $list = array_column($list, null, "id");

        return $list;
    }

    //新增房间边框记录
    public function addRoomFrameLog($frameId, $roomId, $timeSt, $timeEt, $adminId)
    {
        $data = [];
        $data['frame_id'] = (int) $frameId;
        $data['room_id'] = (int) $roomId;
        $data['time_st'] = $timeSt;
        $data['time_et'] = $timeEt;
        $data['admin_id'] = (int) $adminId;

        $id = DB::table($this->roomFrameLogTable)->insertGetId($data);

        $cacheKey = $this->roomFrameRoomKey.$roomId;
        redis::DEL($cacheKey);

        return $id;
    }

    //编辑房间边框记录
    public function editRoomFrameLog($logId, $roomId, $params)
    {
        $upData = [];
        if(!empty($params['time_st'])) {
            $upData['time_st'] = $params['time_st'];
        }
        if(!empty($params['time_et'])) {
            $upData['time_et'] = $params['time_et'];
        }
        if(!empty($params['admin_id'])) {
            $upData['admin_id'] = $params['admin_id'];
        }
        if(isset($params['is_delete'])) {
            $upData['is_delete'] = (int) $params['is_delete'];
        }
        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->roomFrameLogTable)->where('id', $logId)->where('is_delete', 0)->update($upData);

        $cacheKey = $this->roomFrameRoomKey.$roomId;
        redis::DEL($cacheKey);

        return $res;
    }

    /**
     * 根据房间ID获取房间边框列表
     */
    public function getRoomFrameByRoomId($roomId){
        $cacheKey = $this->roomFrameRoomKey.$roomId;
        $list = Redis::get($cacheKey);
        if(empty($list)){
            $list = DB::table($this->roomFrameLogTable)
                ->where('is_delete', 0)
                ->where('room_id', $roomId)
                ->select(['id','room_id','frame_id','time_st','time_et'])
                ->get()->toArray();
            $list = self::objToArray($list);
            Redis::setex($cacheKey, 86400, json_encode($list));
        }else{
            $list = json_decode($list, true);
        }

        return $list;
    }

    /**
     * 根据id获取房间边框记录
     */
    public function getRoomFrameLogById($logId){
        $data = DB::table($this->roomFrameLogTable)
            ->where('is_delete', 0)
            ->where('id', $logId)
            ->first();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 根据时间房间ID正在使用的房间边框
     */
    public function getRoomFrameByRoomIdTime($roomId, $now){
        $result = '';
        do {
            $logList = $this->getRoomFrameByRoomId($roomId);
            if(empty($logList)){
                break;
            }
            $log = [];
            foreach($logList as $val) {
                if(strtotime($val['time_st'])<=$now && $now<=strtotime($val['time_et'])) {
                    $log = $val;
                    break;
                }
            }
            if(!empty($log)) {
                $roomFrame = $this->getRoomFrameAll();
                $roomFrame = array_column($roomFrame, null, 'id');
                if(!empty($roomFrame[$log['frame_id']])) {
                    $result = build_url($roomFrame[$log['frame_id']]['frame_img']);
                }
            }
        } while (false);

        return $result;
    }

}
